package com.community.process._1010.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.process._1010.common.utils.PageUtil;
import com.community.process._1010.common.utils.mp.MpPageFormat;
import com.community.process._1010.dao.entity.ProcessFoodItem;
import com.community.process._1010.dao.entity.ProcessIngredients;
import com.community.process._1010.dao.mapper.ProcessFoodItemMapper;
import com.community.process._1010.dao.service.ProcessCategoryIService;
import com.community.process._1010.dao.view.ProcessFoodItemView;
import com.community.process._1010.dao.vo.ProcessFoodItemIngredientsVo;
import com.community.process._1010.dao.vo.ProcessFoodItemVo;
import com.community.process._1010.dao.vo.ProcessIngredientsVo;
import com.community.process._1010.domains.ProcessFoodItemDTO;
import com.community.process._1010.service.ProcessFoodItemIngredientsService;
import com.community.process._1010.service.ProcessFoodItemService;
import com.oneadds.lowcode.BaseServiceImpl;
import com.oneadds.lowcode.dto.PageListData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: Administrator to Zbs
 * @Date: 2024/9/6 星期五 13:09
 */
@Service
@RequiredArgsConstructor
public class ProcessFoodItemServiceImpl extends BaseServiceImpl<ProcessFoodItemMapper, ProcessFoodItem> implements ProcessFoodItemService {

    private final ProcessFoodItemIngredientsService processFoodItemIngredientsService;
    private final ProcessCategoryIService processCategoryIService;

    @Override
    public ProcessFoodItemView getProcessFoodItemView(Long id) {
        ProcessFoodItem processFoodItem = this.getById(id);
        List<Long> parentIdsByChildId = processCategoryIService.findParentIdsByChildId(processFoodItem.getCategoryId());
        List<ProcessFoodItemIngredientsVo> processFoodItemIngredientsVoList = processFoodItemIngredientsService.getProcessFoodItemIngredientsVoList(id);
        return new ProcessFoodItemView(processFoodItem, parentIdsByChildId,processFoodItemIngredientsVoList);
    }

    @Override
    public PageListData<ProcessFoodItemVo> getProcessFoodItemVoPageList(ProcessFoodItemDTO.FoodItemPageSearch dto) {
        List<Long> foodItemIdList = null;
        // 推荐菜品查询
        if (!CollectionUtils.isEmpty(dto.getIngredientsIdList())) {
            // 查询食材是否存在于菜品中
            foodItemIdList = processFoodItemIngredientsService.getFoodItemIdListByInIngredientsId(dto.getIngredientsIdList());
            if (!CollectionUtils.isEmpty(foodItemIdList)) {
                return new PageListData<>(dto.getPageSize(),0L,new ArrayList<>());
            }
        }
        // yg_process_food_item ypfi 成品菜
        // yg_process_category ypc 分类
        QueryWrapper<ProcessFoodItemVo> wrapper = new QueryWrapper<>();
        wrapper.eq(dto.getCategoryId() != null,"ypfi.category_id", dto.getCategoryId());
        wrapper.in(!CollectionUtils.isEmpty(foodItemIdList),"ypfi.id", foodItemIdList);
        wrapper.eq(dto.getUp() != null,"ypfi.up", dto.getUp());
        wrapper.like(StrUtil.isNotBlank(dto.getName()),"ypfi.`name`", dto.getName());
        wrapper.likeRight(StrUtil.isNotBlank(dto.getCategoryTreeCode()),"ypc.`tree_code`", dto.getCategoryTreeCode());
        wrapper.eq("ypfi.deleted", 0);
        wrapper.eq("ypc.deleted", 0);
        wrapper.orderByDesc("ypfi.`up`","ypfi.`create_time`");
        Page<ProcessFoodItemVo> page = this.baseMapper.getProcessFoodItemVoPageList(PageUtil.page(dto), wrapper);

//        List<Long> foodItemIds = page.getRecords().stream().map(ProcessFoodItemVo::getId).collect(Collectors.toList());
//        List<ProcessFoodItemIngredientsVo> foodItemIngredientsVoList = processFoodItemIngredientsService.getProcessFoodItemIngredientsVoListByInFoodItemId(foodItemIds);
//        Map<Long, List<ProcessFoodItemIngredientsVo>> foodItemIngredientsVoMap = foodItemIngredientsVoList.stream().collect(Collectors.groupingBy(ProcessFoodItemIngredientsVo::getFoodItemId));
//
//        List<ProcessFoodItemVo> collect = page.getRecords().stream().peek(record -> record.setIngredients(foodItemIngredientsVoMap.getOrDefault(record.getId(), null))).collect(Collectors.toList());
//        return new PageListData<>(page.getSize(),page.getTotal(),collect);
        return MpPageFormat.format(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProcessFoodItem(ProcessFoodItemDTO.FoodItemAdd dto) {
        ProcessFoodItem processFoodItem = new ProcessFoodItem(dto);
        this.save(processFoodItem);
        // 添加菜品食材
        processFoodItemIngredientsService.addFoodItemIngredients(processFoodItem.getId(),dto.getIngredients());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProcessFoodItem(ProcessFoodItemDTO.FoodItemUpdate dto) {
        this.updateById(new ProcessFoodItem(dto));
        // 删除菜品食材
        processFoodItemIngredientsService.deleteFoodItemIngredients(dto.getId());
        // 添加菜品食材
        processFoodItemIngredientsService.addFoodItemIngredients(dto.getId(),dto.getIngredients());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProcessFoodItem(ProcessFoodItemDTO.FoodItemDelete dto) {
        this.removeByIds(dto.getIds());
    }

    @Override
    public void setUp(ProcessFoodItemDTO.FoodItemSetUp dto) {
        this.lambdaUpdate().in(ProcessFoodItem::getId, dto.getIds())
                .set(ProcessFoodItem::getUp, dto.getUp())
                .update();
    }
}
